package com.ludomancy.starwave;

import java.awt.Graphics;

import com.ludomancy.gamelib.PositionInterpolator;

public class MoveAnimation implements Animation
{
	private PositionInterpolator interpolator;
	private Ship ship;
	private int toX;
	private int toY;
	private boolean started = false;
	
	public MoveAnimation(Ship ship, int tox, int toy)
	{
		toX = tox;
		toY = toy;
		
		int pixelX = ship.px*Settings.TILE_SIZE;
		int pixelY = ship.py*Settings.TILE_SIZE;
		int targetPixelX = tox*Settings.TILE_SIZE;
		int targetPixelY = toy*Settings.TILE_SIZE;
		
		interpolator = new PositionInterpolator(pixelX, pixelY, targetPixelX, targetPixelY, Settings.MOVE_SHIP_FRAME_TIME);
		this.ship = ship;
		start();
	}
	
	public void start()
	{
		started = true;
	}
	
	public boolean isOver()
	{
		return interpolator.isMoving() == false;
	}
	
	public void update()
	{
		if(started)
		{
			interpolator.update();
			if(!interpolator.isMoving() && ship.isMover())
			{
				// apply the change
				Game.game.moveShip(ship, toX, toY);
				started = false;
			}
		}
	}
	
	public void paint(Graphics g)
	{
		ship.paint(g, interpolator.x, interpolator.y);
	}
	
}
